<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;

class UserCard extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'user_cards';
    protected $fillable = [
        'user_id',
        'card_id',
        'dense',
        'status',
    ];
    public function user()
    {
        return $this->belongsTo(User::class,'user_id','id');
    }
    public function card()
    {
        return $this->belongsTo(CardType::class,'card_id','id');
    }

    public function bindCard($userid, $card_id, $dense)
    {
        $usercardmodel=$this->where('user_id',$userid)->orWhere('dense',$dense)->first();
        if (!is_null($usercardmodel)){
            throw new \Exception('该密钥或者用户已经被绑定');
        }
       return $this->create([
            'user_id'=>$userid,
            'card_id'=>$card_id,
            'dense'=>$dense,
            'status'=>1,
        ]);
    }


}
